package com.cloud.pole.pay.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.HttpClientUtils;
import com.cloud.pole.common.util.security.MD5Utils;
import com.cloud.pole.pay.common.ccbpay.CcbPayConfig;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("payCcbNotify/front")
@Slf4j
public class PayCcbNotifyController {

	@ResponseBody
    @RequestMapping(value = "testPayCcbNotify",method = RequestMethod.GET)
    public ApiRestResponse<Boolean> testPayCcbNotify(
    		@RequestParam(defaultValue = "031040950") String POSID,
    		@RequestParam(defaultValue = "430000000") String BRANCHID,
    		@RequestParam(defaultValue = "") String ORDERID,
    		@RequestParam(defaultValue = "100") String PAYMENT,
    		@RequestParam(defaultValue = "01") String CURCODE,
    		@RequestParam(defaultValue = "") String REMARK1,
    		@RequestParam(defaultValue = "") String REMARK2,
    		@RequestParam(defaultValue = "12") String ACC_TYPE,
    		@RequestParam(defaultValue = "Y") String SUCCESS,
    		@RequestParam(defaultValue = "1") String TYPE,
    		@RequestParam(defaultValue = "") String REFERER,
    		@RequestParam(defaultValue = "") String CLIENTIP,
    		@RequestParam(defaultValue = "") String ACCDATE,
    		@RequestParam(defaultValue = "") String USRMSG,
    		@RequestParam(defaultValue = "") String INSTALLNUM,
    		@RequestParam(defaultValue = "") String ERRMSG,
    		@RequestParam(defaultValue = "") String USRINFO,
    		@RequestParam(defaultValue = "") String DISCOUNT){   	
    	try {    		
			StringBuffer tmp = new StringBuffer();
			tmp.append("POSID=").append(CcbPayConfig.POSID).append("&BRANCHID=").append(CcbPayConfig.BRANCHID)
					.append("&ORDERID=").append(ORDERID).append("&PAYMENT=").append(PAYMENT)
					.append("&CURCODE=").append(CcbPayConfig.CURCODE).append("&REMARK1=").append(REMARK1)
					.append("&REMARK2=").append(REMARK2).append("&ACC_TYPE=").append(ACC_TYPE).append("&SUCCESS=").append("Y");
			tmp.append("&TYPE=").append("1").append("&REFERER=").append(REFERER).append("&CLIENTIP=").append(CLIENTIP);
			if(!StringUtils.isEmpty(ACCDATE)) {
				tmp.append("&ACCDATE=").append(ACCDATE);
			}
			if(!StringUtils.isEmpty(USRMSG)) {
				tmp.append("&USRMSG=").append(USRMSG);
			}
			if(!StringUtils.isEmpty(ERRMSG)) {
				tmp.append("&ERRMSG=").append(ERRMSG);
			}
			String SIGN =  MD5Utils.md5Hex(tmp.toString());
			String strUrl = "http://klz-t.royao.net/index.php/openapi/ectools_payment/parse/ectools_payment_plugin_wapccp/callback?" + tmp + "&SIGN=" + SIGN;
			log.info("访问地址：{}", strUrl);
			

			String strResp = HttpClientUtils.doGet(strUrl);
			log.info("响应参数：{}", strResp);
			
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, Boolean.TRUE);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
